package com.bdqn.jk.service.impl;

import com.bdqn.jk.common.ServiceResultEnum;
import com.bdqn.jk.controller.vo.NewBeeMallIndexCarouselVO;
import com.bdqn.jk.dao.CarouselMapper;
import com.bdqn.jk.entity.Carousel;
import com.bdqn.jk.service.CarouselService;
import com.bdqn.jk.util.BeanUtil;
import com.bdqn.jk.util.PageQueryUtil;
import com.bdqn.jk.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

@Service
public class CarouselServiceImpl implements CarouselService {

    private CarouselMapper carouselMapper;
    @Override
    public PageResult getCarouselPage(PageQueryUtil pageUtil) {
        List<Carousel> carousels = carouselMapper.findCarouselList(pageUtil);
        int total = carouselMapper.getTotalCarousels(pageUtil);
        PageResult pageResult=new PageResult(carousels,total,pageUtil.getLimit(),pageUtil.getPage());
        return pageResult;
    }

    @Override
    public String saveCarousel(Carousel carousel) {
        if (carouselMapper.insertall(carousel)>0){
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public String updateCarousel(Carousel carousel) {
        Carousel ca=carouselMapper.selectById(carousel.getCarouselId());
        if (ca == null){
            return ServiceResultEnum.DATA_NOT_EXIST.getResult();
        }
        ca.setCarouselRank(carousel.getCarouselRank());
        ca.setRedirectUrl(carousel.getRedirectUrl());
        ca.setCarouselUrl(carousel.getCarouselUrl());
        ca.setCreateTime(new Date());
        if(carouselMapper.updateAllById(ca)>0){
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public Carousel getCarouselById(Integer id) {
        return carouselMapper.selectById(id);
    }

    @Override
    public Boolean deleteBatch(Integer[] ids) {
        if (ids.length < 1){
            return false;
        }
        return carouselMapper.deleteBatch(ids)>0;
    }

    @Override
    public List<NewBeeMallIndexCarouselVO> getCarouselsForIndex(int number) {
        List<NewBeeMallIndexCarouselVO> nb= new ArrayList<>(number);
        List<Carousel> carousels = carouselMapper.findCarouselById(number);
        if (!CollectionUtils.isEmpty(carousels)){
            nb = BeanUtil.copyList(carousels,NewBeeMallIndexCarouselVO.class);
        }
        return nb;
    }
}
